<!-- 忘记密码短信重置 -->
<template>
	<view class="uni-content pb20">
		<view class="pt20 pb20">
			<uni-steps :options="stepList" :active="stepActive" />
		</view>
		<uni-forms ref="resetPwdForm" :value="resetPwdFormVal" err-show-type="toast">
			<uni-row v-if="stepActive == 0">
				<uni-forms-item>
					<uni-easyinput :focus="focusPhonenumber" @blur="focusPhonenumber = false" v-model="phonenumber"
						placeholder="请输入系统预留手机号码" />
				</uni-forms-item>
				<uni-forms-item>
					<uni-row :gutter="10">
						<uni-col :span="12">
							<uni-easyinput :focus="focusYzm" @blur="focusYzm = false" class="input-box" v-model="yzm"
								placeholder="请输入验证码" />
						</uni-col>
						<uni-col :span="11" :push="1">
							<button type="primary" plain class="fs14" size="default"
								@click="checkPhoneAndYzm()">发送验证码</button>
						</uni-col>
					</uni-row>
				</uni-forms-item>
				<button class="blue-btn" type="default" @click="checkPhoneAndYzm(1)">提 交</button>
			</uni-row>
			<uni-row v-if="stepActive == 1">
				<uni-forms-item name="newPassword">
					<uni-easyinput class="input-box" type="password" v-model="resetPwdFormVal.newPassword"
						placeholder="请输入新密码">
					</uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="newPassword2">
					<uni-easyinput class="input-box" type="password" v-model="resetPwdFormVal.newPassword2"
						placeholder="请再次输入新密码">
					</uni-easyinput>
				</uni-forms-item>
				<button class="blue-btn" type="default" @click="submit">提 交</button>
			</uni-row>
		</uni-forms>
		<!-- 忘记密码 -->
		<view class="link-box">
			<text class="link-box-left"></text>
			<text class="link-box-right" @click="navigateToFun('/pages/ucenter/forgetPwd/index')">切换邮箱验证</text>
		</view>
	</view>
</template>

<script>
	import passwordMod from '../common/js/passwordForChange.js'
	export default {
		data() {
			return {
				"phonenumber": "",
				"yzm": "",
				"focusPhonenumber": false,
				"focusYzm": false,
				"stepActive": 0,
				"stepList": [{
					"title": '手机验证'
				}, {
					"title": '重置密码'
				}],
				"resetPwdFormVal": {
					'newPassword': '',
					'newPassword2': '',
				},
				rules: {
					...passwordMod.getPwdRules('newPassword', 'newPassword2')
				}
			}
		},
		onReady() {
			this.$refs.resetPwdForm.setRules(this.rules)
		},
		onShow() {},
		methods: {
			checkPhoneAndYzm(type) { //校验手机号  type=1需校验验证码
				const phonenumber = this.phonenumber
				if (phonenumber) {
					const reg = /^1[3-9]\d{9}$/
					if (!reg.test(phonenumber)) {
						this.focusPhonenumber = true
						uni.showToast({
							title: '请输入正确的11位手机号码',
							icon: 'none'
						});
						return false
					} else {
						//判断手机号在系统中是否存在
						let data = {
							phonenumber: phonenumber
						}
						this.$post(this.$api.wx.ucenter.phonenumberIsExist, data).then(res => {
							if (res.isExist) {
								if (type == 1) {
									if (this.yzm) {
										if (this.yzm == "123456") {
											this.stepActive = 1
										} else {
											uni.showToast({
												title: '验证码错误',
												icon: 'none'
											});
										}
									} else {
										this.focusYzm = true
										uni.showToast({
											title: '请输入验证码',
											icon: 'none'
										});
									}
								} else {
									uni.showToast({
										title: '发送成功',
										icon: 'success'
									});
								}
							} else {
								uni.showToast({
									title: "此手机号不是系统预留手机号码",
									icon: 'none'
								});
							}
						}).catch(e => {
							uni.showToast({
								title: e.data.msg,
								icon: 'none'
							});
						})
					}
				} else {
					this.focusPhonenumber = true
					uni.showToast({
						title: '请输入系统预留手机号码',
						icon: 'none'
					});
				}
			},
			submit() { //提交新密码
				this.$refs.resetPwdForm.validate()
					.then(res => {
						uni.showLoading({
							title: '提交中'
						});
						let data = {
							phonenumber: this.phonenumber,
							passwordForRegister: this.resetPwdFormVal.newPassword
						}
						this.$post(this.$api.wx.ucenter.findPwdReset, data).then(res => {
							uni.showToast({
								title: "重置成功",
								icon: 'success'
							});
							setTimeout(() => {
								this.navigateToFun('/pages/ucenter/bind/index')
							}, 1500)
						}).catch(e => {
							uni.showToast({
								title: e.data.msg,
								icon: 'error'
							});
						})
					}).catch(errors => {
						console.log(errors);
					})
			},
			navigateToFun(url) {
				uni.navigateTo({
					url: url,
					fail(e) {
						console.error(e);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "../common/style/uc-common.scss";
</style>
